package com.bazaaroid.server.gae.core.dto.partner;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

import com.bazaaroid.server.gae.core.dao.partner.CategoryDAO;
import com.bazaaroid.server.gae.core.dao.partner.LocationDAO;
import com.bazaaroid.server.gae.core.dao.partner.PartnerDAO;
import com.bazaaroid.server.gae.core.ioc.GuiceFactory;
import com.bazaaroid.server.gae.core.persistence.model.partner.Category;
import com.bazaaroid.server.gae.core.persistence.model.partner.Location;
import com.bazaaroid.server.gae.core.persistence.model.partner.Partner;
import com.bazaaroid.server.gae.core.persistence.model.partner.Product;
import com.google.appengine.api.datastore.Key;

@XmlRootElement(name = "Product")
public class ProductSimpleDTO {
	private Product product = null;

	public ProductSimpleDTO(){}

	public ProductSimpleDTO(Product product) {
		this.product = product;
	}

	@XmlElement
	public String getKey() {
		return product.getKey().toString();
	}

	@XmlElement
	public String getName() {
		return product.getName();
	}

	@XmlElement
	public String getDescription() {
		return product.getDescription();
	}

	@XmlElement
	public double getPrice() {
		return product.getPrice();
	}

	@XmlElement
	public String getCurrency() {
		return product.getCurrency();
	}

	@XmlElement
	public String getImagePath() {
		return product.getImagePath();
	}

	@XmlElement
	public PartnerDTO getPartner() {
		PartnerDAO partnerDAO = GuiceFactory.getInjector()
			.getInstance(PartnerDAO.class);
		Partner partner = partnerDAO.getPartnerByProduct(product);
		partner.setContact(null);
		return new PartnerDTO(partner);
	}

	@XmlElement(name = "category")
	@XmlElementWrapper(name = "categories")
	public List<CategoryDTO> getCategories() {
		CategoryDAO categoryDAO = GuiceFactory.getInjector()
			.getInstance(CategoryDAO.class);

		List<CategoryDTO> categories = new ArrayList<CategoryDTO>();
		for(Key categoryKey: product.getCategories()) {
			Category category = categoryDAO.getById(categoryKey);
			categories.add(new CategoryDTO(category));
		}

		return categories;
	}
	
	@XmlElement(name = "location")
	@XmlElementWrapper(name = "locations")
	public List<LocationSimpleDTO> getLocations() {
		LocationDAO locationDAO = GuiceFactory.getInjector()
			.getInstance(LocationDAO.class);

		List<LocationSimpleDTO> locations = new ArrayList<LocationSimpleDTO>();
		for(Key locationKey: product.getLocations()) {
			Location location = locationDAO.getById(locationKey);
			locations.add(new LocationSimpleDTO(location));
		}

		return locations;
	}
}
